<?php
/**
 * Created by JetBrains PhpStorm.
 * User: yanweidong
 * Date: 13-5-6
 * Time: 下午3:54
 * To change this template use File | Settings | File Templates.
 */
class common extends actionAbstract {


    /**
     * 构造函数
     * @var array
     */
    function __construct() {
        parent::__construct();
	    
    }
	//供应商登陆
	function login(){
	    $this->loadHelper('extend');
        $this->loadModel('pms','account');
        $this->loadModel('hotel','category');
        $this->loadModel('pms','login');
        $this->loadModel('sys','rolepriv');
        $this->loadModel('sys','menu');
        if($_POST){
            $userName=isset($_POST['username'])?$_POST['username']:'';
            $password=isset($_POST['password'])?$_POST['password']:'';
            $verify=isset($_POST['verify'])?$_POST['verify']:'';
            
            if(!trim($userName)){
                ajaxReturn('', "请输入用户名！", 0);
            }
            if(!trim($password)){
                ajaxReturn('', "请输入密码！", 0);
            }
            if(trim($verify)!=$_SESSION['verifycode_content']){
                ajaxReturn('', "请输入验证码！", 0);
            }
            
            $password=md5($password);
            $sql="select * from pms_account where userName='".$userName."' and password='".$password."'";
            $re=$this->pms->accountModel->fetchRow($sql);
            if(!$re){
                ajaxReturn('', "用户名或密码错误！", 0);
            }else{
                if($re['status']==2){
                    ajaxReturn('', "账户被冻结，请联系客服！", 0);
                }
                
                $data['lastIp']=$_SERVER["REMOTE_ADDR"];
                $data['lastTime']=time();
                $this->pms->accountModel->update($data,$re['id']);
                $arr['pmsid']=$re['id'];
                $arr['logintime']=time();
                $arr['loginip']=$_SERVER["REMOTE_ADDR"];
                $this->pms->loginModel->insert($arr);
                
                //权限查询
                $quanxian_sql="select a.roleId,b.module,b.method,b.menuid,a.status from sys_rolepriv as a left join sys_menu as b on b.menuid=a.menuId where a.roleId=".$re['roleid']." and a.status=2";
                $quanxian = $this->sys->roleprivModel->fetchAll($quanxian_sql);
                $result=array();
                if($quanxian){
	                foreach ($quanxian as $k=>$v){
	                	$result[$k] = $v['module'].'/'.$v['method'];
	                }
                }
                if($re['roleid']>0&&!$result){
                    ajaxReturn('', "没有权限！", 0);
                }
                $_SESSION['superMan']=$re['roleid'];
                $_SESSION['result'] = $result;
                
                if($re['parentid']!=0){
                    $hotel_sql="select id from hotel_category where partnerid=".$re['parentid'];
					$payType=$this->pms->accountModel->fetchRow("select payType from pms_account where id=".$re['parentid']);
					$re['payType']=$payType['payType'];
                }else{
                    $hotel_sql = "select id from hotel_category where partnerid=".$re['id'];
                }
                $hotelid = $this->hotel->categoryModel->fetchRow($hotel_sql);
                
                acl::setCookie("PmsCookie",$re);
                if(isset($_POST['keytag']) && $_POST['keytag']==1){
                    setcookie('PmsName', $_POST['username'], time()+3600*24*7,'/');
                    setcookie('PmsPassword', $_POST['password'], time()+3600*24*7,'/');
                }else{
                    setcookie('PmsName', '', time()-3600*24*7,'/');
                    setcookie('PmsPassword', '', time()-3600*24*7,'/');
                }
                
                //包房商判断
                if($hotelid){
                    $_SESSION['hotel_id']=$hotelid['id'];
                }else{
                    $_SESSION['hotel_id']=0;
                }
                if($re['isbusiness']==1&&!$hotelid){
                    ajaxReturn('url', "登录成功！", 1, '/index.php/partner/merchant/category');
                }
                
            	//权限进入选择
                if($re['roleid']==0||in_array('index/main', $result)){
                	ajaxReturn('url', "登录成功！", 1, '/index.php/partner/index/main');
                }else{
                    ajaxReturn('url', "登录成功！", 1, '/index.php/partner/'.$result[0]);
                }
                
            }
        }
        include $this->loadView();
    }
    
    //忘记密码
    function forget(){
        $this->loadModel('pms','account');
        
        include $this->loadView();
    }
    
    
    //退出登录
    function logout(){
        setcookie("PmsCookie", "", time() - 3600,'/');
        unset($_SESSION);
        header("location:/index.php/partner/common/login");
    }
}